package com.itHsuSE.demo8;

/**
 * @author Hsu琛君珩
 * @ClassName:CircleCalculator
 * @date 2023-12-20
 * @apiNote
 * @Version: v1.0
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CircleCalculator extends JFrame {
    private JTextField radiusInput;
    private JTextField resultOutput;
    private JButton calculateButton;

    public CircleCalculator() {
        createUI();
        setLayout(new FlowLayout());
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Circle Calculator");
    }

    private void createUI() {
        radiusInput = new JTextField(10);
        resultOutput = new JTextField(20);
        resultOutput.setEditable(false);
        calculateButton = new JButton("计算");

        calculateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    double radius = Double.parseDouble(radiusInput.getText());
                    double area = 3.14 * radius * radius;
                    double perimeter = 2 * 3.14 * radius;
                    resultOutput.setText("面积: " + area + ", 周长: " + perimeter);
                } catch (NumberFormatException ex) {
                    resultOutput.setText("请输入有效的半径");
                }
            }
        });

        add(radiusInput);
        add(calculateButton);
        add(resultOutput);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CircleCalculator().setVisible(true);
            }
        });
    }
}
